package edu.uwm.capstone.android.widgets;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;

public class WidgetChangeDetector implements TextWatcher {
	private boolean changed = false;

	public WidgetChangeDetector(ViewGroup paramViewGroup) {
		initialize(paramViewGroup);
	}

	private void initialize(ViewGroup paramViewGroup) {
		Log.d("Diasmart",
				"WidgetChangeDetector " + paramViewGroup.getChildCount());
		int i = 0;
		while (i < paramViewGroup.getChildCount()) {
			Log.d("Diasmart",
					"WidgetChangeDetector " + paramViewGroup.getChildAt(i));
			View childView = paramViewGroup.getChildAt(i);
			if ((childView instanceof Spinner)) {
				Spinner childSpinner = (Spinner) childView;
				childSpinner
						.setOnItemSelectedListener(new ItemSelectedListenerWrapper(
								childSpinner.getOnItemSelectedListener()));
			} else if ((childView instanceof CompoundButton)) {
				((CompoundButton) childView)
						.setOnCheckedChangeListener(new ItemCheckedChangedListener());
			} else if ((childView instanceof EditText)) {
				((EditText) childView).addTextChangedListener(this);
			}
			i++;
		}

	}

	public void afterTextChanged(Editable paramEditable) {
		this.changed = true;
	}

	public void beforeTextChanged(CharSequence paramCharSequence,
			int paramInt1, int paramInt2, int paramInt3) {
	}

	public boolean isChanged() {
		return this.changed;
	}

	public void onTextChanged(CharSequence paramCharSequence, int paramInt1,
			int paramInt2, int paramInt3) {
	}

	public void reset() {
		this.changed = false;
	}

	public void setChanged() {
		this.changed = true;
	}

	private class ItemCheckedChangedListener implements
			CompoundButton.OnCheckedChangeListener {
		private ItemCheckedChangedListener() {
		}

		public void onCheckedChanged(CompoundButton paramCompoundButton,
				boolean paramBoolean) {
			WidgetChangeDetector.this.changed = true;
		}
	}

	private class ItemSelectedListenerWrapper implements
			AdapterView.OnItemSelectedListener {
		private boolean initialized = false;
		private long initializedId = -9223372036854775808L;
		private AdapterView.OnItemSelectedListener original = null;

		public ItemSelectedListenerWrapper(
				AdapterView.OnItemSelectedListener arg2) {
			Object localObject = null;
			this.original = (OnItemSelectedListener) localObject;
		}

		public void onItemSelected(AdapterView<?> paramAdapterView,
				View paramView, int paramInt, long paramLong) {
			if (!this.initialized) {
				this.initialized = true;
				this.initializedId = paramLong;
			}
			do {

				while ((paramLong == this.initializedId) && (this.initialized))
					this.initializedId = -9223372036854775808L;
				WidgetChangeDetector.this.changed = true;
			} while (this.original == null);
			this.original.onItemSelected(paramAdapterView, paramView, paramInt,
					paramLong);
		}

		public void onNothingSelected(AdapterView<?> paramAdapterView) {
			if (this.original != null)
				this.original.onNothingSelected(paramAdapterView);
		}
	}
}